import { http } from '@/utils';

export const getChannels = () => {
  return async (dispatch, getState) => {
    const data = await http.get('channels');

    dispatch({ type: 'article/setChannels', payload: data.channels });
  };
};

export const getArticles = (params = undefined) => {
  return async (dispatch, getState) => {
    const data = await http.get('mp/articles', { params: params });
    dispatch({ type: 'article/setArticles', payload: data });
  };
};

export const delArticle = (id) => {
  return async (dispatch, getState) => {
    await http.delete('mp/articles/' + id);
  };
};

export const addArticle = (data, draft = false) => {
  return async (dispatch, getState) => {
    await http.post(`mp/articles?draft=${false}`, data);
  };
};

//编辑文章

export const editArticle = (data, draft = false) => {
  return async (dispatch, getState) => {
    await await http.put(`mp/articles/${data.id}?draft=${draft}`, data);
  };
};

// 获取文章id

export const getArticle = (id) => {
  return async (dispatch, getState) => {
    const data = await http.get('mp/articles/' + id);
    return data;
  };
};
